import { login, getTokenInfo } from '@/api/oauth'
import { getInfo } from '@/api/user'
import { getDashboard } from '@/api/notification'

import { getToken, setToken, removeToken } from '@/utils/auth'

const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    authorities: [],
    isSuperAdmin: false,
    notification: {}
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, authorities) => {
      state.authorities = authorities
    },
    SET_AUTHORITIES: (state, roles) => {
      state.roles = roles
    },
    SET_IS_SUPER_ADMIN: (state, isSuperAdmin) => {
      state.isSuperAdmin = isSuperAdmin
    },
    SET_NOTIFICATION: (state, notification) => {
      state.notification = notification
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      return new Promise((resolve, reject) => {
        login(username, userInfo.password, 'password', 'web_app', 'barracuda_web_app').then(data => {
          setToken(data.access_token)
          commit('SET_TOKEN', data.access_token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit }) {
      return new Promise((resolve, reject) => {
        getTokenInfo().then(data => {
          if (data.roles && data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_ROLES', data.roles)
            if (data.roles.indexOf('ROLE_SUPER_ADMIN') === -1) {
              commit('SET_IS_SUPER_ADMIN', false)
            } else {
              commit('SET_IS_SUPER_ADMIN', true)
            }
          } else {
            reject('getInfo: roles must be a non-null array !')
          }
          if (data.authorities && data.authorities.length > 0) { // 验证返回的authorities是否是一个非空数组
            commit('SET_AUTHORITIES', data.authorities)
          } else {
            reject('getInfo: authorities must be a non-null array !')
          }
          commit('SET_NAME', data.username)
          resolve(data)
        }).catch(error => {
          reject(error)
        })
        getInfo().then(data => {
          if (data.avatar) {
            commit('SET_AVATAR', data.avatar)
          }
        }).catch(error => {
          console.log(error)
        })
      })
    },

    // 登出
    /*
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_REFRESH_TOKEN', '')
          removeToken()
          removeRefreshToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    }, */

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    },

    // 修改头像
    ChangeAvatar({ commit }, avatar) {
      return new Promise(resolve => {
        commit('SET_AVATAR', avatar)
        resolve()
      })
    },

    // 获取通知消息
    GetNotification({ commit }) {
      return new Promise((resolve, reject) => {
        getDashboard().then(data => {
          commit('SET_NOTIFICATION', data)
          resolve(data)
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}

export default user
